#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cjson.h"

char *makeJson()
{
    cJSON *pJsonRoot = NULL;

    pJsonRoot = cJSON_CreateObject();
    if(NULL == pJsonRoot)
    {
        //error happend here
        return NULL;
    }
    cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");
    cJSON_AddNumberToObject(pJsonRoot, "number", 10010);
    cJSON_AddBoolToObject(pJsonRoot, "bool", 1);
    cJSON *pSubJson = NULL;
    pSubJson = cJSON_CreateObject();
    if(NULL == pSubJson)
    {
        // create object faild, exit
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");
    cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);

    char *p = cJSON_Print(pJsonRoot);
    // else use :
    // char * p = cJSON_PrintUnformatted(pJsonRoot);
    if(NULL == p)
    {
        //convert json list to string faild, exit
        //because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coredump, and error is : double free
        cJSON_Delete(pJsonRoot);
        return NULL;
    }
    //free(p);

    cJSON_Delete(pJsonRoot);

    return p;
}

void parseJson(char *pMsg)
{
    if(NULL == pMsg)
    {
        return;
    }
    cJSON *pJson = cJSON_Parse(pMsg);
    if(NULL == pJson)
    {
        // parse faild, return
        return ;
    }

    // get string from json
    cJSON *pSub = cJSON_GetObjectItem(pJson, "hello");
    if(NULL == pSub)
    {
        //get object named "hello" faild
    }
    printf("obj_1 : %s\n", pSub->valuestring);

    // get number from json
    pSub = cJSON_GetObjectItem(pJson, "number");
    if(NULL == pSub)
    {
        //get number from json faild
    }
    printf("obj_2 : %d\n", pSub->valueint);

    // get bool from json
    pSub = cJSON_GetObjectItem(pJson, "bool");
    if(NULL == pSub)
    {
        // get bool from json faild
    }
    printf("obj_3 : %d\n", pSub->valueint);

    // get sub object
    pSub = cJSON_GetObjectItem(pJson, "subobj");
    if(NULL == pSub)
    {
        // get sub object faild
    }
    cJSON *pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");
    if(NULL == pSubSub)
    {
        // get object from subject object faild
    }
    printf("sub_obj_1 : %s\n", pSubSub->valuestring);

    cJSON_Delete(pJson);
}
/*
int main()
{
	char * p = makeJson();
	if(NULL == p)
	{
		return 0;
	}
	printf("%s\n", p);
	printf("-----------------------------------------------------------------\n");
	parseJson(p);
	free(p);//千万不要忘记释放内存呀，cJSON_Print()函数或者cJSON_PrintUnformatted（）产生的内存，使用free(char *)进行释放
	return 0;
}
*/
/***********************************************************************/
/********************创建一个键值对*************************************
int main()
{
	cJSON *root = NULL;
	cJSON *item = NULL;
	char *out = NULL;

	root = cJSON_CreateObject();
	item = cJSON_CreateString("Brett");
        cJSON_AddItemToObject(root,"firstName",item);
	out = cJSON_Print(root);

	printf("out = [%s]\n", out);

	//释放cJSON_CreateObject()分配出来的内存空间
	cJSON_Delete(root);
	//释放cJSON_Print ()分配出来的内存空间
	free(out);

	return 0;
}
*************************************************************************/
/***************************************************************/
/*********转换一个结构体****************************************/
typedef struct
{
    char firstName[32];
    char lastName[32];
    char email[64];
    int age;
    float height;
} PERSON;
int main()
{
    PERSON Person;
    cJSON *root = NULL;
    cJSON *obj_person = NULL;
    cJSON *item = NULL;
    PERSON *person = &Person;
    char *out = NULL;

    memset(&Person, 0x00, sizeof(PERSON));
    sprintf(person->firstName, "fname");
    sprintf(person->lastName, "lname");
    sprintf(person->email, "11111111111@139.com");
    person->age = 23;
    person->height = 175.0;

    root = cJSON_CreateObject();
    obj_person = cJSON_CreateObject();

    item = cJSON_CreateString(person->firstName);
    cJSON_AddItemToObject(obj_person, "firstName", item);
    item = cJSON_CreateString(person->lastName);
    cJSON_AddItemToObject(obj_person, "lastName", item);
    item = cJSON_CreateString(person->email);
    cJSON_AddItemToObject(obj_person, "email", item);
    item = cJSON_CreateNumber(person->age);
    cJSON_AddItemToObject(obj_person, "age", item);
    item = cJSON_CreateNumber(person->height);
    cJSON_AddItemToObject(obj_person, "height", item);

    cJSON_AddItemToObject(root, "person", obj_person);

    out = cJSON_Print(root);
    printf("out = [%s]\n", out);

    cJSON_Delete(root);
    free(out);

    return 0;
}

